package net.onceface.web.resources;

import javax.inject.Inject;
import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
/**
 * 订单资源
 * **/
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import net.onceface.application.OrderApplication;
import net.onceface.application.commons.InvokeResult;
import net.onceface.application.commons.Message;
import net.onceface.application.dto.OrderDTO;
/**
 * 订单资源
 * **/
@Path("/order")
public class OrderResources extends BaseResource{

	@Inject
	private OrderApplication application;
	
	/**
	 * 获得订单详情
	 * **/
	@GET
	@Path("/getDetail")
	@Consumes({MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public String get(@QueryParam("orderNumber")final String orderNumber){
		try{
			return toDateJson(InvokeResult.success(application.getDetailByOrderNumber(orderNumber)));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.GET_ORDER_FAILURE));
		}
	}
	
	/**
	 * 按状态获取用户订单
	 * **/
	@GET
	@Path("/list")
	@Consumes({MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public String list(@QueryParam("clientId")final Long clientId,
						@QueryParam("status")final String status){
		try{
			return toDateJson(InvokeResult.success(application.findOrderByClient(clientId, status)));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.QUERY_ORDER_FAILURE));
		}
	}
	
	/**
	 * 用户取消订单
	 * **/
	@POST
	@Path("/cancelOrder")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public InvokeResult cancelOrder(@FormParam("clientId") final Long clientId,
								@FormParam("id") final Long id){
		try{
			return application.cancelOrder(clientId, id);
		}catch(Exception e){
			return InvokeResult.failure(Message.CANCEL_ORDER_FAILURE);
		}
	}
	
	/**
	 * 订单分析
	 * **/
	@POST
	@Path("/orderAnalysis")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public String orderAnalysis(@FormParam("clientId") final Long clientId,
								@FormParam("disCountId") final Long disCountId){
		try{
			return toJson(InvokeResult.success(application.orderAnalysis(clientId, disCountId)));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.ORDER_ANALYSIS_FAILURE));
		}
	}
	/**
	 * 提交订单
	 * **/
	@POST
	@Path("/placeOrder")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public InvokeResult placeOrder(@BeanParam OrderDTO orderDTO){
		try{
			return application.placeOrder(orderDTO);
		}catch(Exception e){
			return InvokeResult.failure(Message.PLACE_ORDER_FAILURE);
		}
	}
}
